<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Common\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Container\ContainerInterface;
use \Hyperf\Contract\SessionInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use \Hyperf\View\RenderInterface;

abstract class AbstractController
{
    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject()
     * @var RenderInterface
     */
    protected $render;

    /**
     * @Inject()
     * @var SessionInterface
     */
    protected $session;

    /**
     * @Inject
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @Inject
     * @var ResponseInterface
     */
    protected $response;

    final public function success($data = [], $msg = '', $extends_data = [], $requestType = 'api',$code = 0)
    {
        switch ($requestType){
            case 'api':
                $returnRes = [
                    'code' => $code,
                    'msg' => $msg,
                    'data' => $data
                ];
                $returnRes = array_merge($returnRes, $extends_data);
                return $this->response->json($returnRes);
                break;
            case 'http':
                $returnRes = [
                    'code' => $code,
                    'msg' => $msg,
                    'data' => !empty($data['url'])?$data['url']:'/',
                    'wait' => !empty($data['wait'])?$data['wait']:'3',
                ];
                $param = base64_encode(json_encode($returnRes));
                return $this->response->redirect('/jump?param='.$param);
                break;
        }
    }


    final public function error($msg = '', $data = [], $requestType = 'api', $code = 400)
    {
        switch ($requestType){
            case 'api':
                $returnRes = [
                    'code' => $code,
                    'msg' => $msg,
                    'data' => $data
                ];
                return $this->response->json($returnRes);
                break;
            case 'http':
                $returnRes = [
                    'code' => $code,
                    'msg' => $msg,
                    'data' => !empty($data['url'])?$data['url']:'/',
                    'wait' => !empty($data['wait'])?$data['wait']:'3',
                ];
                $param = base64_encode(json_encode($returnRes));
                return $this->response->redirect('/jump?param='.$param);
                break;
        }
    }

    /**
     * 使用验证器注解、并且开启严格过滤模式后，这里取到的数据是安全的
     * @return array|object|null
     */
    public function validated()
    {
        return $this->request->getParsedBody();
    }

}
